package com.qdxwx.web.config;


import com.qdxwx.common.config.UploadConfig;
import com.qdxwx.web.interceptor.AuthInterceptor;
import com.qdxwx.web.interceptor.WebsiteInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ZhiYong
 * @createTime 2020/10/19
 * @description
 */
@Configuration
public class QdxMvcConfigurer implements WebMvcConfigurer {

    @Bean
    public WebsiteInterceptor webSiteInterceptor() {
        return new WebsiteInterceptor();
    }

    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:" + UploadConfig.uploadRoot + "/");
    }

    /**
     * 拦截器按照顺序执行
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/pay/**")
                .addPathPatterns("/user/**");
        registry.addInterceptor(webSiteInterceptor())
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/ajax/**")
                .excludePathPatterns("/upload/**")
                .addPathPatterns("/**");
    }
}
